<?php
/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */
namespace Admin\Controller;
use Admin\Model\ArticleModel;
class ArticleController extends BaseController {
    public function articleList() {
        $Article = M('Article');
        $res = $list = array();
        $p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
        $size = empty($_REQUEST['size']) ? 20 : $_REQUEST['size'];
        $where = " 1 = 1 ";
        $keywords = trim(I('keywords'));
        $keywords && $where.=" and title like '%$keywords%' ";
        $res = $Article->where($where)->order('article_id desc')->page("$p,$size")->select();
        $count = $Article->where($where)->count();
        $pager = new \Think\Page($count, $size);
        $page = $pager->show();
        $this->assign('list', $res);
        $this->assign('page', $page);
        $this->display();
    }
    public function article() {
        $ArticleMod = new ArticleModel();
        $act = I('GET.act', 'add');
        $info = array();
        $info['publish_time'] = time() + 3600 * 24;
        if (I('GET.article_id')) {
            $article_id = I('GET.article_id');
            $info = D('article')->where('article_id=' . $article_id)->find();
			$article_img_mod = M('article_img');
			$article_img = $article_img_mod->where(array('aid'=>$article_id))->select();
			$this->assign('article_img', $article_img);
        }
        $this->assign('info', $info);
        $this->initEditor();
		$region = $this->getCate('region');
		$this->assign('region', $region);
		$this->assign('act', $act);
        $this->display();
    }
    /**
     * 初始化编辑器链接
     * @param $post_id post_id
     */
    private function initEditor() {
        $this->assign("URL_upload", U('Admin/Ueditor/imageUp', array('savepath' => 'article')));
        $this->assign("URL_fileUp", U('Admin/Ueditor/fileUp', array('savepath' => 'article')));
        $this->assign("URL_scrawlUp", U('Admin/Ueditor/scrawlUp', array('savepath' => 'article')));
        $this->assign("URL_getRemoteImage", U('Admin/Ueditor/getRemoteImage', array('savepath' => 'article')));
        $this->assign("URL_imageManager", U('Admin/Ueditor/imageManager', array('savepath' => 'article')));
        $this->assign("URL_imageUp", U('Admin/Ueditor/imageUp', array('savepath' => 'article')));
        $this->assign("URL_getMovie", U('Admin/Ueditor/getMovie', array('savepath' => 'article')));
        $this->assign("URL_Home", "");
    }
    public function aticleHandle() {
        $data = I('post.');
        if ($data['act'] == 'add') {
            unset($data['act'], $data['__hash__']);
            $data['click'] = mt_rand(1000, 1300);
            $data['up_time'] = $data['add_time'] = time();
			$data['activity_start_time'] = strtotime($data['activity_start_time']);
			$data['activity_end_time'] = strtotime($data['activity_end_time']);
			unset($data['article_id']);
			$r = D('article')->add($data);
			if($r && count($data['info_images'])){
				$article_img_mod = M('article_img');
				foreach((array)array_filter($data['info_images']) as $k=>$v){
					if($k==0){
						D('article')->where('article_id=' . $r)->save(array('images'=>$v));
					}
					$data_img['aid'] = $r;
					$data_img['images'] = $v;
					$article_img_mod->add($data_img);
				}
			}
        }
        if ($data['act'] == 'edit') {
            unset($data['act'], $data['__hash__']);
			$data['activity_start_time'] = strtotime($data['activity_start_time']);
			$data['activity_end_time'] = strtotime($data['activity_end_time']);
            $data['up_time'] = time();
			$r = D('article')->where('article_id=' . $data['article_id'])->save($data);
			if($r && count($data['info_images'])){
				$article_img_mod = M('article_img');
				foreach((array)array_filter($data['info_images']) as $k=>$v){
					if($k==0){
						D('article')->where('article_id=' . $data['article_id'])->save(array('images'=>$v));
					}
					$check = $article_img_mod->where(array('images'=>$v))->count();
					if(!$check){
						$data_img['aid'] = $data['article_id'];
						$data_img['images'] = $v;
						$article_img_mod->add($data_img);
					}
				}
			}
        }
        if ($data['act'] == 'del') {
            $r = D('article')->where('article_id=' . $data['article_id'])->delete();
            if ($r)
                exit(json_encode(1));
        }
        $referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Article/articleList');
        if ($r) {
            $this->success("操作成功", $referurl);
        } else {
            $this->error("操作失败", $referurl);
        }
    }
    public function link() {
        $act = I('GET.act', 'add');
        $this->assign('act', $act);
        $link_id = I('GET.link_id');
        $link_info = array();
        if ($link_id) {
            $link_info = D('friend_link')->where('link_id=' . $link_id)->find();
            $this->assign('info', $link_info);
        }
        $this->display();
    }
    public function linkList() {
        $Ad = M('friend_link');
        $res = $Ad->where('1=1')->order('orderby')->page($_GET['p'] . ',10')->select();
        if ($res) {
            foreach ($res as $val) {
                $val['target'] = $val['target'] > 0 ? '开启' : '关闭';
                $list[] = $val;
            }
        }
        $this->assign('list', $list);
        $count = $Ad->where('1=1')->count();
        $Page = new \Think\Page($count, 10);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->display();
    }
    public function linkHandle() {
        $data = I('post.');
        if ($data['act'] == 'add') {
            stream_context_set_default(array('http' => array('timeout' => 2)));
            send_http_status('311');
            $r = D('friend_link')->add($data);
        }
        if ($data['act'] == 'edit') {
            $r = D('friend_link')->where('link_id=' . $data['link_id'])->save($data);
        }
        if ($data['act'] == 'del') {
            $r = D('friend_link')->where('link_id=' . $data['link_id'])->delete();
            if ($r)
                exit(json_encode(1));
        }
        if ($r) {
            $this->success("操作成功", U('Admin/Article/linkList'));
        } else {
            $this->error("操作失败", U('Admin/Article/linkList'));
        }
    }
    public function helpList() {
    }
    public function noticeList() {
    }
}
